#include "Splash.h"
#include "MainMenu.h"
#include "CCConfiguration.h"

CCScene* Splash::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        Splash *layer = Splash::node();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool Splash::init()
{
	
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());
       
        // Get window size 
        CCSize windowSize = CCDirector::sharedDirector()->getWinSize();
        //set background
        CCSprite* pSprite = CCSprite::spriteWithFile("res/load.png");
        CC_BREAK_IF(! pSprite);
        pSprite->setPosition(ccp(windowSize.width/2, windowSize.height/2));
        this->addChild(pSprite, 0);

		//set blink text
		CCLabelTTF* clickLabel = CCLabelTTF::labelWithString("Click any key to continue", "Thonburi", 40);
		CC_BREAK_IF(!clickLabel);
		clickLabel->setPosition(ccp(windowSize.width / 2, windowSize.height / 4));
		CCActionInterval*  action = CCBlink::actionWithDuration(10, 10);
		CCRepeatForever *repeat = CCRepeatForever::actionWithAction(action);
		clickLabel->runAction(repeat);
		this->addChild(clickLabel, 1);
		
        bRet = true;
    } while (0);

    return bRet;
}

bool Splash::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	//when it's touched, skip the splash and go to MainMenu
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::transitionWithDuration(1, MainMenu::scene()));
	return true;
}

void Splash::onEnter()
{
	CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);
	CCLayer::onEnter();
}

void Splash::onExit()
{
	CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}